Public Class CusOrderList
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents PageSelect1 As SR.WebControls.PageSelect
    Protected WithEvents txtOrderNo As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnSearch As System.Web.UI.WebControls.Button
    Protected WithEvents dgOrders As System.Web.UI.WebControls.DataGrid
    Protected WithEvents lblTotal As System.Web.UI.WebControls.Label
    Protected WithEvents lblTotalText As System.Web.UI.WebControls.Label
    Protected WithEvents Pageselect2 As SR.WebControls.PageSelect
    Protected WithEvents ddlSortRange As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlSortRef As System.Web.UI.WebControls.DropDownList
    Protected WithEvents RegularExpressionValidator1 As System.Web.UI.WebControls.RegularExpressionValidator
    Protected WithEvents ddlStatus As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlSupplier As System.Web.UI.WebControls.DropDownList
    Protected WithEvents lblSortRef1 As System.Web.UI.WebControls.Label
    Protected WithEvents lblSortRef2 As System.Web.UI.WebControls.Label


    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected WithEvents Header1 As Header

    Private ORDER_INPUT_URL As String = "/Internal/OrderInput.aspx"
    Private PAGE_SIZE As Integer = 20


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Me.IsPostBack Then
            Security.CustomerOnly()

            Header1.NavigationBar.Add("Create new order", AppRoot() & ORDER_INPUT_URL)
            Header1.MenuIndex = EMenuIndex.CustomerOrderList
            'Header1.MenuIndex = 3

            Dim curCustomer As CUser
            curCustomer = CType(Session(Security.CURRENT_USER_SESSION_NAME), CUser)
            Me.lblSortRef1.Text = HttpUtility.HtmlEncode(curCustomer.SortReference1)
            Me.lblSortRef2.Text = HttpUtility.HtmlEncode(curCustomer.SortReference2)

            Me.ddlStatus.DataSource = EnumToList(GetType(EOrderStatus))
            Me.ddlStatus.DataTextField = "Text"
            Me.ddlStatus.DataValueField = "Value"
            Me.ddlStatus.DataBind()

            ' get supplier list of all customer's orders
            Me.ddlSupplier.DataSource = COrder.GetSupplierListFromOrders(Security.GetCurrentUser.UsIdPk, False)
            Me.ddlSupplier.DataTextField = "SUPPLIER_CODE"
            Me.ddlSupplier.DataValueField = "LKD_ID_PK"
            Me.ddlSupplier.DataBind()
            Me.ddlSupplier.Items.Insert(0, New ListItem("[All]", "0"))

            'From now on, we can see the SortRange as Sort Ref. 2
            Me.ddlSortRange.DataSource = COrder.GetSortRangeListFromOrders(Security.GetCurrentUser.UsIdPk)
            Me.ddlSortRange.DataTextField = "SORT_REF2_CODE"
            Me.ddlSortRange.DataValueField = "LKD_ID_PK"
            Me.ddlSortRange.DataBind()
            Me.ddlSortRange.Items.Insert(0, New ListItem("[All]", "0"))

            Me.ddlSortRef.DataSource = COrder.GetSortRefListFromOrders(Security.GetCurrentUser.UsIdPk)
            Me.ddlSortRef.DataTextField = "SORT_REF_CODE"
            Me.ddlSortRef.DataValueField = "LKD_ID_PK"
            Me.ddlSortRef.DataBind()
            Me.ddlSortRef.Items.Insert(0, New ListItem("[All]", "0"))

            If ddlSupplier.Items.Count > 0 Then
                Me.PageSelect1.PageSize = Me.PAGE_SIZE
                Me.Pageselect2.PageSize = Me.PageSelect1.PageSize

                Me.BindGrid()
            Else
                Me.PageSelect1.Visible = False
                Me.Pageselect2.Visible = False
                Me.lblTotal.Text = "0"
                Me.lblTotalText.Text = "ORDER(S)"
            End If
        End If
    End Sub

    Private Sub BindGrid()
        'get order list 
        'return a dataset with 2 tables
        'the first one will contain order list
        'the first column of first row of the second one will contain total available order
        Dim objOrder As New COrder
        Dim ds As DataSet = objOrder.GetOrderListByCusIDSupIDSortRefSortRangeOrderNo(Security.GetCurrentUser.UsIdPk, CInt(Me.ddlSupplier.SelectedItem.Value), CInt(Me.ddlSortRef.SelectedItem.Value), CInt(Me.ddlSortRange.SelectedItem.Value), Me.txtOrderNo.Text, CByte(Me.ddlStatus.SelectedItem.Value), Me.PageSelect1.CurrentPageIndex * Me.PageSelect1.PageSize, Me.PageSelect1.PageSize)

        Me.dgOrders.DataSource = ds.Tables(0)
        Me.dgOrders.DataBind()

        Dim cnt As Integer = CInt(ds.Tables(1).Rows(0)(0))

        Me.lblTotal.Text = cnt.ToString

        'If cnt > 0 Then
        '    Me.lblTotalText.Text = "ORDERS"
        'Else
        Me.lblTotalText.Text = "ORDER(S)"
        'End If

        'create top page select
        Me.PageSelect1.ItemCount = cnt
        Me.PageSelect1.Create()

        'create bottom page select and sync with top page select
        Me.Pageselect2.ItemCount = Me.PageSelect1.ItemCount
        Me.Pageselect2.Create()
    End Sub

    Private Sub PageSelect1_PageIndexChanged(ByVal newPageIndex As Integer) Handles PageSelect1.PageIndexChanged
        Me.PageSelect1.CurrentPageIndex = newPageIndex
        Me.Pageselect2.CurrentPageIndex = newPageIndex
        Me.BindGrid()
    End Sub

    Private Sub Pageselect2_PageIndexChanged(ByVal newPageIndex As Integer) Handles Pageselect2.PageIndexChanged
        Me.PageSelect1.CurrentPageIndex = newPageIndex
        Me.Pageselect2.CurrentPageIndex = newPageIndex
        Me.BindGrid()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Me.PageSelect1.CurrentPageIndex = 0
        Me.Pageselect2.CurrentPageIndex = 0
        Me.BindGrid()
    End Sub

    Protected Function GetQuantityText(ByVal container As Object) As String
        Dim noContainer As Integer = CInt(DataBinder.Eval(container, "DataItem.CONTAINERS"))
        Dim type As EContainerType = CType(DataBinder.Eval(container, "DataItem.CONTAINER_TYPE"), EContainerType)
        Return String.Format("{0}x{1}", noContainer, type.ToString.Replace("_", " "))
    End Function
End Class
